package com.zhl.media.app.mvp.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.utils.ArmsUtils;
import com.zhl.media.app.R;
import com.zhl.media.app.model.MediaPhoto;
import com.zhl.media.commonsdk.imgaEngine.config.CommonImageConfigImpl;

import java.util.ArrayList;
import java.util.List;

public class UploadGridViewAdapter extends BaseAdapter {
    private AppComponent mAppComponent;
    private ImageLoader mImageLoader;//用于加载图片的管理类,默认使用 Glide,使用策略模式,可替换框架
    private Context context;                        //上下文对象用于视图填充
    private List<MediaPhoto> data;       //需要适配的数据源
    public List<View> viewList;                    //View对象集合

    public UploadGridViewAdapter(Context context, List<MediaPhoto> data) {
        this.viewList = new ArrayList<>();
        this.context = context;
        this.data = data;
        mAppComponent = ArmsUtils.obtainAppComponentFromContext(context);
        mImageLoader = mAppComponent.imageLoader();

    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.upload_gridview_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
            viewHolder.overImg = (ImageView) convertView.findViewById(R.id.overImg);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final MediaPhoto mediaPhoto = data.get(position);

        if(mediaPhoto.getUploader()){
            viewHolder.overImg.setVisibility(View.VISIBLE);
        }else {
            viewHolder.overImg.setVisibility(View.GONE);
        }
        mImageLoader.loadImage(context, CommonImageConfigImpl.builder().url(mediaPhoto.getPhotoPath()).imageView(viewHolder.img).build());
        convertView.setTag(R.id.gv, position);
        viewList.add(convertView);
        return convertView;
    }

    public class ViewHolder {
        public ImageView img;
        public ImageView overImg;
    }

}
